package com.goufn.permission.controller;

import com.goufn.permission.annotation.Log;
import com.goufn.permission.common.page.PageRequest;
import com.goufn.permission.common.result.CommonResult;
import com.goufn.permission.model.SysUser;
import com.goufn.permission.service.UserService;
import com.goufn.permission.utils.PasswordUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @Log("新增/修改用户")
    @RequiresPermissions({"sys:user:add", "sys:user:edit"})
    @PostMapping(value="/save")
    public CommonResult save(@RequestBody SysUser record) {
        SysUser user = userService.findById(record.getId());
        if(user != null) {
            if("admin".equalsIgnoreCase(user.getName())) {
                return CommonResult.error("超级管理员不允许修改!");
            }
        }
        if(record.getPassword() != null) {
            if(user == null) {
                // 新增用户
                if(userService.findByName(record.getName()) != null) {
                    return CommonResult.error("用户名已存在!");
                }
                PasswordUtil.encryptPassword(record);
            } else {
                // 修改用户, 且修改了密码
                if(!record.getPassword().equals(user.getPassword())) {
                    PasswordUtil.encryptPassword(record);
                }
            }
        }
        return CommonResult.success(userService.save(record));
    }

    @Log("删除用户")
    @RequiresPermissions("sys:user:delete")
    @PostMapping(value="/delete")
    public CommonResult delete(@RequestBody List<SysUser> records) {
        for(SysUser record : records) {
            SysUser sysUser = userService.findById(record.getId());
            if(sysUser != null && "admin".equalsIgnoreCase(sysUser.getName())) {
                return CommonResult.error("超级管理员不允许删除!");
            }
        }
        return CommonResult.success(userService.delete(records));
    }

    @RequiresPermissions("sys:user:view")
    @GetMapping(value="/findByName")
    public CommonResult findByUserName(@RequestParam String name) {
        return CommonResult.success(userService.findByName(name));
    }

    @RequiresPermissions("sys:user:view")
    @GetMapping(value="/findPermissions")
    public CommonResult findPermissions(@RequestParam String name) {
        return CommonResult.success(userService.findPermissions(name));
    }

    @RequiresPermissions("sys:user:view")
    @GetMapping(value="/findUserRoles")
    public CommonResult findUserRoles(@RequestParam Long userId) {
        return CommonResult.success(userService.findUserRoles(userId));
    }

    @Log("查看用户")
    @RequiresPermissions("sys:user:view")
    @PostMapping(value="/findPage")
    public CommonResult findPage(@RequestBody PageRequest pageRequest) {
        return CommonResult.success(userService.findPage(pageRequest));
    }

    @Log("更新用户头像")
    @PostMapping("/updateAvatar")
    public CommonResult updateAvatar(@RequestBody SysUser user) {
        SysUser existUser = userService.findById(user.getId());
        if (existUser == null) {
            return CommonResult.error("用户不存在");
        }
        existUser.setAvatar(user.getAvatar());
        boolean success = userService.save(existUser);
        if (success) {
            return CommonResult.success("更新头像成功");
        }
        return CommonResult.error("更新头像失败");
    }

    @Log("更新个人信息")
    @PostMapping("/updateProfile")
    public CommonResult updateProfile(@RequestBody SysUser user) {
        SysUser existUser = userService.findById(user.getId());
        if (existUser == null) {
            return CommonResult.error("用户不存在");
        }
        existUser.setEmail(user.getEmail());
        existUser.setMobile(user.getMobile());
        boolean success = userService.save(existUser);
        if (success) {
            return CommonResult.success("更新成功");
        }
        return CommonResult.error("更新失败");
    }

    @Log("修改密码")
    @PostMapping("/updatePassword")
    public CommonResult updatePassword(@RequestBody Map<String, String> params) {
        String username = params.get("name");
        String oldPassword = params.get("oldPassword");
        String newPassword = params.get("newPassword");
        
        SysUser user = userService.findByName(username);
        if (user == null) {
            return CommonResult.error("用户不存在");
        }
        
        // 验证旧密码
        String oldEncryptedPassword = PasswordUtil.encryptPassword(oldPassword, user.getSalt());
        if (!user.getPassword().equals(oldEncryptedPassword)) {
            return CommonResult.error("原密码不正确");
        }
        
        // 设置新密码
        user.setPassword(newPassword);
        PasswordUtil.encryptPassword(user);
        
        boolean success = userService.save(user);
        if (success) {
            return CommonResult.success("密码修改成功");
        }
        return CommonResult.error("密码修改失败");
    }
}
